import codecs
import struct

def decode_shift_jis(buf):
  try:
    return buf.decode("shift_jis")
  except:
    return ""

def handle_buffer(buf):
  return decode_shift_jis(buf).encode("utf-8").decode("utf-8")

def read_string(fp):
  ret = b''
  while True:
    temp = fp.read(1)
    if temp == b'':
      return ''
    ret = ret + temp
    if temp == b'\x00':
      break
  return ret

def main():
  elf_file = open("code.bin", "rb")
  out = open("code.bin.txt", "w", encoding='utf-8')
  while True:
    data = read_string(elf_file)
    if type(data) == str:
      break
    if data == b'' or data == b'\x00':
      continue
    out.write("offset=0x%X\n" % (elf_file.tell() - len(data)))
    out.write("original=%s\n" % handle_buffer(data))
    out.write("target=\n------\n")

if __name__ == "__main__":
  main()

